/**
 *
 */
if (Tapestry.FieldEventManager) {

	var VALIDATION_ERROR_CLASSNAME = "validation-error";
	var VALIDATION_MSG_CLASSNAME = "validation-msg";

	Tapestry.FieldEventManager.addMethods( {
		showValidationMessage : function(message) {
			$T(this.field).validationError = true;
			$T(this.field.form).validationError = true;
	
			//Although tying the 'field' to 'this' makes no sense, I'm assuming other parts of T5 scripting might need it...
	        var field = this.field;
	
			//Like above, the this. specs I'm using only because I suspect some other T5 scripts may depend on it.
	        var id = this.field.id;
	        var lbl = this.label = $(id + '-label');
	        if (lbl) {
		        var parentNode = lbl.up();

				parentNode.addClassName(VALIDATION_ERROR_CLASSNAME);
				
				var hasMessage = false;
	
				parentNode.select("." + VALIDATION_MSG_CLASSNAME).each(function(n) {
					if (!hasMessage && n.firstChild && n.firstChild.nodeValue && n.firstChild.nodeValue == message) hasMessage = true;
				});
	
				//Only add message if this specific one hasn't been added so far...
				if (!hasMessage) {
					var p = document.createElement("p");
					p.className = VALIDATION_MSG_CLASSNAME + " msg" + id;
					p.appendChild(document.createTextNode(message));
					parentNode.appendChild(p);
				}

	        } else {
	        	alert("JavaScript ERROR: Label is required to use SimpleValidationDecorator!!!");
	        }

		},
		removeDecorations : function() {

			var field = this.field;
			var t = $T(field);
	        var value = $F(this.field);
	        var validationError = t.validationError || (value.blank() && this.requiredCheck); //Since I noticed required doesn't get handled in quite the same way (don't ask me why, it's a T5 mystery)

			//In defiance of the original validator I don't want to hide the decorations when the user focusses a different field...
			if (!validationError) {
				field.removeClassName(VALIDATION_ERROR_CLASSNAME);
				
				var parentNode = field.up();
				
				parentNode.select("." + VALIDATION_MSG_CLASSNAME).each(function(n) {
					parentNode.removeChild(n);
				});
				
				parentNode.removeClassName(VALIDATION_ERROR_CLASSNAME);
			}

		}
	});

}

//alert("Modifying validator support for more simple and elegant validation output!");